package starsservice.com.kc.controller;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import starsservice.com.kc.Util.ResponseJson;
import starsservice.com.kc.bean.SecretProtection;
import starsservice.com.kc.service.IAccountService;
import starsservice.com.kc.service.ISecretProtectionService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Api(value = "个人的有关接口", tags = "personal", description = "个人信息有关的接口")
public class PersonalController {

    @Autowired
    private ISecretProtectionService secretProtectionService;

    @Autowired
    private IAccountService accountService;

    @Resource
    private ResponseJson responseJson;

    @ApiOperation(value = "添加个人安全密保接口", notes = "{\n" +
            "    \"questionOne\":\"问题一\",\n" +
            "    \"answerOne\":\"答案一\",\n" +
            "    \"questionTwo\":\"问题二\",\n" +
            "    \"answerTwo\":\"答案二\",\n" +
            "    \"questionThree\":\"问题三\",\n" +
            "    \"answerThree\":\"答案三\"\n" +
            "}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "success"),
            @ApiResponse(code = 400, message = "error")
    })
    @PostMapping("/personal/addsecurity")
    public ResponseJson addSecurity(@RequestBody SecretProtection secretProtection, HttpServletRequest request) {
        String token = request.getHeader("AccessToken");
        return secretProtectionService.addSecurity(secretProtection, token);
    }


    @ApiOperation(value = "更换头像接口", notes = "一次只能上传一张，异步处理,不能大于5MB")
    @ApiImplicitParam(name = "file", value = "文件上传", required = true, dataTypeClass = MultipartFile.class)
    @RequestMapping(value = "/personal/updateavatar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, method = RequestMethod.POST)
    public void updateAvatar(MultipartFile file, HttpServletRequest request) {
        String token = request.getHeader("AccessToken");
        if (file.getSize() > ((1048576 * 5))) {
            return;
        }
        accountService.updateAvatar(file, token);
    }

}
